using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CaptureScreenshot : MonoBehaviour
{
	public static CaptureScreenshot instance;

	public CanvasScaler cs;

	public Texture2D iconTexture;

	public int paddingX;

	public int paddingY;

	private Texture2D screenshotWithLogoTexture;

	private Texture2D screenshotTexture;

	private Color[][] iconPixels;

	public Texture2D ScreenshotWithLogoTexture
	{
		get
		{
			return screenshotWithLogoTexture;
		}
	}

	public Texture2D ScreenshotTexture
	{
		get
		{
			return screenshotTexture;
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
		float num = (float)Screen.width / cs.referenceResolution.x;
		paddingX = (int)((float)paddingX * num);
		paddingY = (int)((float)paddingY * num);
		int width = iconTexture.width;
		int height = iconTexture.height;
		iconTexture = ScaledTexture(iconTexture, (int)((float)width * num), (int)((float)height * num));
		Color[] pixels = iconTexture.GetPixels();
		iconPixels = new Color[iconTexture.width][];
		for (int i = 0; i < iconTexture.width; i++)
		{
			iconPixels[i] = new Color[iconTexture.height];
			for (int j = 0; j < iconTexture.height; j++)
			{
				iconPixels[i][j] = pixels[i + j * iconTexture.width];
			}
		}
	}

	public void TakeScreenshotWithLogo()
	{
		StartCoroutine(TakeScreenshotWithLogoCoroutine());
	}

	public IEnumerator TakeScreenshotWithLogoCoroutine()
	{
		if (screenshotWithLogoTexture != null)
		{
			Object.Destroy(screenshotWithLogoTexture);
		}
		yield return new WaitForEndOfFrame();
		int width = Screen.width;
		int height = Screen.height;
		screenshotWithLogoTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
		screenshotWithLogoTexture.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
		int i = 0;
		int j = 0;
		for (i = 0; i < iconPixels.Length; i++)
		{
			for (j = 0; j < iconPixels[i].Length; j++)
			{
				screenshotWithLogoTexture.SetPixel(paddingX + i, paddingY + j, Color.Lerp(screenshotWithLogoTexture.GetPixel(paddingX + i, paddingY + j), iconPixels[i][j], iconPixels[i][j].a / 1f));
			}
		}
		screenshotWithLogoTexture.Apply();
	}

	public void TakeScreenshot()
	{
		StartCoroutine(TakeScreenshotCoroutine());
	}

	public IEnumerator TakeScreenshotCoroutine()
	{
		if (screenshotTexture != null)
		{
			Object.Destroy(screenshotTexture);
		}
		yield return new WaitForEndOfFrame();
		int width = Screen.width;
		int height = Screen.height;
		screenshotTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
		screenshotTexture.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
		screenshotTexture.Apply();
	}

	public static Texture2D DublicateTexture(Texture2D src)
	{
		Rect rect = new Rect(0f, 0f, src.width, src.height);
		Texture2D texture2D = new Texture2D(src.width, src.height, TextureFormat.ARGB32, true);
		texture2D.SetPixels32(src.GetPixels32());
		return texture2D;
	}

	public static Texture2D ScaledTexture(Texture2D src, int width, int height, FilterMode mode = FilterMode.Trilinear)
	{
		Rect source = new Rect(0f, 0f, width, height);
		GPUScale(src, width, height, mode);
		Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, true);
		texture2D.Resize(width, height);
		texture2D.ReadPixels(source, 0, 0, true);
		return texture2D;
	}

	private static void GPUScale(Texture2D src, int width, int height, FilterMode fmode)
	{
		src.filterMode = fmode;
		src.Apply(true);
		RenderTexture renderTarget = new RenderTexture(width, height, 32);
		Graphics.SetRenderTarget(renderTarget);
		GL.LoadPixelMatrix(0f, 1f, 1f, 0f);
		GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
		Graphics.DrawTexture(new Rect(0f, 0f, 1f, 1f), src);
	}
}
